package com.fdb.efp.nls.api.service;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.cdp.pboc.service.facade.CreditQueryDetailService;
import com.fdb.cdp.pboc.service.facade.ZXHtmlInfoService;
import com.fdb.cdp.pboc.service.vo.CreditQueryDetailVO;
import com.fdb.cdp.pboc.service.vo.ZXHtmlInfoVO;
import com.fdb.efp.esb.service.bo.req.basicfn.QueryRscWarnLoanReqBean;
import com.fdb.efp.esb.service.bo.req.basicfn.RscWarnLoanDatasReqBean;
import com.fdb.efp.esb.service.bo.resp.basicfn.QueryRscWarnLoanRespBean;
import com.fdb.efp.esb.service.bo.resp.basicfn.RscWarnLoanDatasRespBean;
import com.fdb.efp.esb.service.facade.basicfn.QueryRscWarnLoanService;
import com.fdb.efp.esb.service.facade.basicfn.RscWarnLoanDatasService;
import com.fdb.efp.nls.service.facade.AccLoanService;
import com.fdb.efp.nls.service.vo.AccLoanVO;
import com.fdb.efp.nls.service.facade.RscWarnGnlHistDataService;
import com.fdb.efp.nls.service.facade.RscWarnGnlHistRuleInfoService;
import com.fdb.efp.nls.service.facade.RscWarnLoanService;
import com.fdb.efp.nls.service.vo.RscWarnGnlHistDataVO;
import com.fdb.efp.nls.service.vo.RscWarnGnlHistRuleInfoVO;
import com.fdb.efp.nls.service.vo.RscWarnLoanVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 需求编号：【2019D0509】（业务提的需求编号）
 * 问题编号：【】（若不是因为修复问题而新增该类，则留空）
 * 开发人员：【wanglh4】
 * 创建日期：【2019/11/20 9:23】
 * 功能描述：
 */
@Service("rscWarnLoanProcessService")
public class RscWarnLoanProcessService {

    @Autowired
    @Qualifier("rscWarnLoanDatasService")
    private RscWarnLoanDatasService rscWarnLoanDatasService;
    @Autowired
    @Qualifier("queryRscWarnLoanService")
    private QueryRscWarnLoanService queryRscWarnLoanService;
    @Autowired
    @Qualifier("rscWarnGnlHistDataService")
    private RscWarnGnlHistDataService rscWarnGnlHistDataService;
    @Autowired
    @Qualifier("rscWarnGnlHistRuleInfoService")
    private RscWarnGnlHistRuleInfoService rscWarnGnlHistRuleInfoService;
    @Autowired
    @Qualifier("rscWarnLoanService")
    private RscWarnLoanService rscWarnLoanService;
    @Autowired
    @Qualifier("creditQueryDetailService")
    private CreditQueryDetailService creditQueryDetailService;
    @Autowired
    @Qualifier("zxHtmlInfoService")
    private ZXHtmlInfoService zxHtmlInfoService;
    @Autowired
    @Qualifier("accLoanService")
    private AccLoanService accLoanService;


    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(RscWarnLoanProcessService.class);

    public RscWarnLoanDatasRespBean getHistRscWarnLoanDatas(RscWarnLoanVO inRscWarnLoanVo) throws Exception {
        String reqId = UUID.randomUUID().toString().replace("-","");
        RscWarnLoanDatasReqBean rscWarnLoanDatasReqBean = new RscWarnLoanDatasReqBean();
        rscWarnLoanDatasReqBean.setCustId(inRscWarnLoanVo.getCusId());
        rscWarnLoanDatasReqBean.setCustNm(inRscWarnLoanVo.getCusName());
        rscWarnLoanDatasReqBean.setIdentNo(inRscWarnLoanVo.getCertCode());
        rscWarnLoanDatasReqBean.setIdentTp(inRscWarnLoanVo.getCertType());
        rscWarnLoanDatasReqBean.setRqsIdmptntID(reqId);
        rscWarnLoanDatasReqBean.setCardNo("");
        logger.info("查询条件为：" +  JSONObject.toJSONString(rscWarnLoanDatasReqBean));
        RscWarnLoanDatasRespBean rs = rscWarnLoanDatasService.applyRscWarnLoanDatas(rscWarnLoanDatasReqBean);
        logger.info("查询结果为：" +  JSONObject.toJSONString(rs));
        return rs;
    }

    public int dealWithRscWarnGnlVal(String cusId, RscWarnLoanDatasRespBean rs) throws Exception {
        //组装通过接口获取到的数据
        RscWarnGnlHistDataVO rscWarnGnlHistDataVO = accessableRs(rs);
        //组装数据
        List<RscWarnGnlHistRuleInfoVO> ruleInfos = accessableReDatas(rscWarnGnlHistDataVO);
        //批量更新
        int outputVo = batchUpdate(ruleInfos);
        return outputVo;
    }

    public int batchUpdate(List<RscWarnGnlHistRuleInfoVO> ruleInfos) throws Exception {
        logger.info("批量更新的数据为：" + JSONObject.toJSONString(ruleInfos));
        int num = rscWarnGnlHistRuleInfoService.updateByRuleCode(ruleInfos);
        if (num < 0) throw new Exception("将“风控数据信息规则名称表”做为临时表更新数据失败！");
        logger.info("批量更新结束！");
        return num;
    }

    public List<RscWarnGnlHistRuleInfoVO> accessableReDatas(RscWarnGnlHistDataVO rscWarnGnlHistDataRs) throws Exception {
        logger.info("开始组装数据,将“风控数据信息规则名称表”做为临时表更新数据！");
        //获取规则名规则号对照表信息
        RscWarnGnlHistRuleInfoVO queryRscWarnGnlHistRuleInfoVO = new RscWarnGnlHistRuleInfoVO();
        List<RscWarnGnlHistRuleInfoVO> ruleInfos = rscWarnGnlHistRuleInfoService.queryByRuleCode(queryRscWarnGnlHistRuleInfoVO);
        logger.info("查询到的“风控数据信息规则名称表”数据为：" + JSONObject.toJSONString(ruleInfos));
        //将查询到的实体类组装成字段名：字段值的map
        Map<String,String> rscWarnGnlHistDataRsMap = new HashMap();
        try {
            logger.info("返回实体类映射处理至Map开始！数据为：" + JSONObject.toJSONString(rscWarnGnlHistDataRs));
            Class rscWarnGnlHistDataRsClass = rscWarnGnlHistDataRs.getClass();
            Field[] field = rscWarnGnlHistDataRsClass.getDeclaredFields();
            for (int i = 0; i < field.length; i++) {
                Field f = field[i];
                boolean accessFlag = f.isAccessible();
                f.setAccessible(true);
                if (f.get(rscWarnGnlHistDataRs) == null) {
                    rscWarnGnlHistDataRsMap.put(f.getName(), "");
                } else {
                    rscWarnGnlHistDataRsMap.put(f.getName(), f.get(rscWarnGnlHistDataRs).toString());
                }
                f.setAccessible(accessFlag);
            }
        } catch (Exception e) {
            logger.error("数据映射失败！" + e.getMessage());
            throw new Exception("数据映射处理失败！");
        }
        //处理数据,更新规则结果
        logger.info("返回实体类映射处理至Map结束，开始为“风控数据信息规则名称表”数据赋值！");
        for (Map.Entry<String,String> entry: rscWarnGnlHistDataRsMap.entrySet()) {
            for (RscWarnGnlHistRuleInfoVO item : ruleInfos) {
                if (entry.getKey().toLowerCase().indexOf(item.getRuleCode().toLowerCase()) != -1) {
                    if (entry.getKey().length() > item.getRuleCode().length()) {
                        item.setCurrValue(entry.getValue());
                    } else {
                        item.setHistValue(entry.getValue());
                    }
                }
            }
        }
        logger.info("赋值处理结束，结果为：" + JSONObject.toJSONString(ruleInfos));
        return ruleInfos;
    }

    public RscWarnGnlHistDataVO accessableRs(RscWarnLoanDatasRespBean rs) {
        logger.info("开始将接口返回的数据组装成相应的对象！返回信息为：" + JSONObject.toJSONString(rs));
        RscWarnGnlHistDataVO rscWarnGnlHistDataRs = new RscWarnGnlHistDataVO();
        rscWarnGnlHistDataRs.setRqsIdmptntID(rs.getRqsIdmptntID());
        rscWarnGnlHistDataRs.setCusId(rs.getCustId());
        rscWarnGnlHistDataRs.setCusName(rs.getCustNm());
        rscWarnGnlHistDataRs.setCertType(rs.getIdentTp());
        rscWarnGnlHistDataRs.setCardNo(rs.getCardNo());
        rscWarnGnlHistDataRs.setIdCardHitCrtLoseCrdtList(rs.getQryGnlHistDataOld().getIdCardHitCrtLoseCrdtList());
        rscWarnGnlHistDataRs.setIdCardHitCrmList(rs.getQryGnlHistDataOld().getIdCardHitCrmList());
        rscWarnGnlHistDataRs.setIdCardHitCrtExecList(rs.getQryGnlHistDataOld().getIdCardHitCrtExecList());
        rscWarnGnlHistDataRs.setMblNoHitFalNoLib(rs.getQryGnlHistDataOld().getMblNoHitFalNoLib());
        rscWarnGnlHistDataRs.setMblNoHitHghRskAttnList(rs.getQryGnlHistDataOld().getMblNoHitHghRskAttnList());
        rscWarnGnlHistDataRs.setMblNoHitArsCoLglRprsList(rs.getQryGnlHistDataOld().getMblNoHitArsCoLglRprsList());
        rscWarnGnlHistDataRs.setThreeMosIdCardLnkMultFlg(rs.getQryGnlHistDataOld().getThreeMosIdCardLnkMultFlg());
        rscWarnGnlHistDataRs.setSevenDayAppAplcMulBrrwFlg(rs.getQryGnlHistDataOld().getSevenDayAppAplcMulBrrwFlg());
        rscWarnGnlHistDataRs.setOneMosAplcMultPlatAppBrrwFlg(rs.getQryGnlHistDataOld().getOneMosAplcMultPlatAppBrrwFlg());
        rscWarnGnlHistDataRs.setThreeMosAplcMultPlatAppBrrwFlg(rs.getQryGnlHistDataOld().getThreeMosAplcMultPlatAppBrrwFlg());
        rscWarnGnlHistDataRs.setHitCardCntrPBCPshBlklist(rs.getQryGnlHistDataOld().getHitCardCntrPBCPshBlklist());
        rscWarnGnlHistDataRs.setHitCardCntrHPCExecPrsnList(rs.getQryGnlHistDataOld().getHitCardCntrHPCExecPrsnList());
        rscWarnGnlHistDataRs.setHitCardCntrHPCLoseCrdtPrsnList(rs.getQryGnlHistDataOld().getHitCardCntrHPCLoseCrdtPrsnList());
        rscWarnGnlHistDataRs.setHitCardBnkFrdBlklist(rs.getQryGnlHistDataOld().getHitCardBnkFrdBlklist());
        rscWarnGnlHistDataRs.setHitCardBnkSzLmtRskFlgBlklist(rs.getQryGnlHistDataOld().getHitCardBnkSzLmtRskFlgBlklist());
        rscWarnGnlHistDataRs.setHitCardCntrP2PLoseCrdtList(rs.getQryGnlHistDataOld().getHitCardCntrP2PLoseCrdtList());
        rscWarnGnlHistDataRs.setHitCardCntrBnkRskCardBlklist(rs.getQryGnlHistDataOld().getHitCardCntrBnkRskCardBlklist());
        rscWarnGnlHistDataRs.setHitCardCntrAbnTxnBlklist(rs.getQryGnlHistDataOld().getHitCardCntrAbnTxnBlklist());
        rscWarnGnlHistDataRs.setHitCardCntrLoanRskMrkBlklist(rs.getQryGnlHistDataOld().getHitCardCntrLoanRskMrkBlklist());
        rscWarnGnlHistDataRs.setHitCardCntrHistQryIndvBlklist(rs.getQryGnlHistDataOld().getHitCardCntrHistQryIndvBlklist());
        rscWarnGnlHistDataRs.setHitCardCntrHistQrySmShldGrylst(rs.getQryGnlHistDataOld().getHitCardCntrHistQrySmShldGrylst());
        rscWarnGnlHistDataRs.setHitCardCntrPBCPublcInfBadList(rs.getQryGnlHistDataOld().getHitCardCntrPBCPublcInfBadList());
        rscWarnGnlHistDataRs.setHitCardCntrFinBsnM4CustList(rs.getQryGnlHistDataOld().getHitCardCntrFinBsnM4CustList());
        rscWarnGnlHistDataRs.setHitHeadOfcBlklist(rs.getQryGnlHistDataOld().getHitHeadOfcBlklist());
        rscWarnGnlHistDataRs.setHitHeadOfcGrylst(rs.getQryGnlHistDataOld().getHitHeadOfcGrylst());
        rscWarnGnlHistDataRs.setHitAntiMnyLndrngMontrngList(rs.getQryGnlHistDataOld().getHitAntiMnyLndrngMontrngList());
        rscWarnGnlHistDataRs.setHitIDEAHistRfsList(rs.getQryGnlHistDataOld().getHitIDEAHistRfsList());
        rscWarnGnlHistDataRs.setOwnBnkLoanCrnOdueRcrd(rs.getQryGnlHistDataOld().getOwnBnkLoanCrnOdueRcrd());
        rscWarnGnlHistDataRs.setOwnBnkLoantHghstOdueOvr60Day(rs.getQryGnlHistDataOld().getOwnBnkLoantHghstOdueOvr60Day());
        rscWarnGnlHistDataRs.setLoanCrnStOdueFlg(rs.getQryGnlHistDataOld().getLoanCrnStOdueFlg());
        rscWarnGnlHistDataRs.setCrCardDiscvrFrzSpclFlg(rs.getQryGnlHistDataOld().getCrCardDiscvrFrzSpclFlg());
        rscWarnGnlHistDataRs.setCrCardDiscvrStopPymtSpclFlg(rs.getQryGnlHistDataOld().getCrCardDiscvrStopPymtSpclFlg());
        rscWarnGnlHistDataRs.setQscrdtDiscvrFrzSpclFlg(rs.getQryGnlHistDataOld().getQscrdtDiscvrFrzSpclFlg());
        rscWarnGnlHistDataRs.setQscrdtDiscvrStopPymtSpclFlg(rs.getQryGnlHistDataOld().getQscrdtDiscvrStopPymtSpclFlg());
        rscWarnGnlHistDataRs.setCrCard12MosOvr6TmsOdueFlg(rs.getQryGnlHistDataOld().getCrCard12MosOvr6TmsOdueFlg());
        rscWarnGnlHistDataRs.setQscrdt12MosOvr6TmsOdueFlg(rs.getQryGnlHistDataOld().getQscrdt12MosOvr6TmsOdueFlg());
        rscWarnGnlHistDataRs.setCrCard12MosOvr6TmsOdueRcrdFlg(rs.getQryGnlHistDataOld().getCrCard12MosOvr6TmsOdueRcrdFlg());
        rscWarnGnlHistDataRs.setCrCardRctly12MnthCtns3TmsOvOdue(rs.getQryGnlHistDataOld().getCrCardRctly12MnthCtns3TmsOvOdue());
        rscWarnGnlHistDataRs.setQscrdtRctly12MnthCtns3TmsOvOdue(rs.getQryGnlHistDataOld().getQscrdtRctly12MnthCtns3TmsOvOdue());
        rscWarnGnlHistDataRs.setLoan12MosOvr3TmsOdueRcrdFlg(rs.getQryGnlHistDataOld().getLoan12MosOvr3TmsOdueRcrdFlg());
        rscWarnGnlHistDataRs.setLoan12MnthHghstOdueTrmOvr2Tms(rs.getQryGnlHistDataOld().getLoan12MnthHghstOdueTrmOvr2Tms());
        rscWarnGnlHistDataRs.setLoan24MnthHghstOdueTrmOvr3Tms(rs.getQryGnlHistDataOld().getLoan24MnthHghstOdueTrmOvr3Tms());
        rscWarnGnlHistDataRs.setCrCard12MnthHghstOdueTrmOvr2Tms(rs.getQryGnlHistDataOld().getCrCard12MnthHghstOdueTrmOvr2Tms());
        rscWarnGnlHistDataRs.setQscrdt12MnthHghstOdueTrmOvr2Tms(rs.getQryGnlHistDataOld().getQscrdt12MnthHghstOdueTrmOvr2Tms());
        rscWarnGnlHistDataRs.setCrCard24MnthHghstOdueTrmOvr3Tms(rs.getQryGnlHistDataOld().getCrCard24MnthHghstOdueTrmOvr3Tms());
        rscWarnGnlHistDataRs.setQscrdt24MnthHghstOdueTrmOvr3Tms(rs.getQryGnlHistDataOld().getQscrdt24MnthHghstOdueTrmOvr3Tms());
        rscWarnGnlHistDataRs.setCRCRprtOwndFlg(rs.getQryGnlHistDataOld().getCRCRprtOwndFlg());
        rscWarnGnlHistDataRs.setCrRfrncRprtOwndHistTxnDataFlg(rs.getQryGnlHistDataOld().getCrRfrncRprtOwndHistTxnDataFlg());
        rscWarnGnlHistDataRs.setAplcEtrNatnlCrtLoseCrdtBeExecPrsnList(rs.getQryGnlHistDataOld().getAplcEtrNatnlCrtLoseCrdtBeExecPrsnList());
        rscWarnGnlHistDataRs.setPBCRprtInclAdmntvPnyRcrd(rs.getQryGnlHistDataOld().getPBCRprtInclAdmntvPnyRcrd());
        rscWarnGnlHistDataRs.setLoanSpclTxnTpInclDbtFlg(rs.getQryGnlHistDataOld().getLoanSpclTxnTpInclDbtFlg());
        rscWarnGnlHistDataRs.setRlvnRpymtRspblLvl5ClAttnSecdySspcsLss(rs.getQryGnlHistDataOld().getRlvnRpymtRspblLvl5ClAttnSecdySspcsLss());
        rscWarnGnlHistDataRs.setNotEndCvlExecRcrdFlg(rs.getQryGnlHistDataOld().getNotEndCvlExecRcrdFlg());
        rscWarnGnlHistDataRs.setRctlyExecRcrdFlngTmNotOvr180Day(rs.getQryGnlHistDataOld().getRctlyExecRcrdFlngTmNotOvr180Day());
        rscWarnGnlHistDataRs.setNonCnslAcctCrCardRctly6MnthAvUseLmt(rs.getQryGnlHistDataOld().getNonCnslAcctCrCardRctly6MnthAvUseLmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getNonCnslAcctCrCardRctly6MnthAvUseLmt()));
        rscWarnGnlHistDataRs.setNonCnslAcctQscrdt6MnthAvUseLmt(rs.getQryGnlHistDataOld().getNonCnslAcctQscrdt6MnthAvUseLmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getNonCnslAcctQscrdt6MnthAvUseLmt()));
        rscWarnGnlHistDataRs.setNonCnslAcctCrCardCrTotAmt(rs.getQryGnlHistDataOld().getNonCnslAcctCrCardCrTotAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getNonCnslAcctCrCardCrTotAmt()));
        rscWarnGnlHistDataRs.setNonCnslAcctQscrdtCrTotAmt(rs.getQryGnlHistDataOld().getNonCnslAcctQscrdtCrTotAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getNonCnslAcctQscrdtCrTotAmt()));
        rscWarnGnlHistDataRs.setAllCrCardCrTotAmt(rs.getQryGnlHistDataOld().getAllCrCardCrTotAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getAllCrCardCrTotAmt()));
        rscWarnGnlHistDataRs.setAllQscrdtCrTotAmt(rs.getQryGnlHistDataOld().getAllQscrdtCrTotAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getAllQscrdtCrTotAmt()));
        rscWarnGnlHistDataRs.setUtlzRt(rs.getQryGnlHistDataOld().getUtlzRt());
        rscWarnGnlHistDataRs.setCrTotAmt(rs.getQryGnlHistDataOld().getCrTotAmt() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataOld().getCrTotAmt()));
        rscWarnGnlHistDataRs.setCRCRprtLstUdtRsdcAddr(rs.getQryGnlHistDataOld().getCRCRprtLstUdtRsdcAddr());
        rscWarnGnlHistDataRs.setCRCRprtLstUdtCoAddrSt(rs.getQryGnlHistDataOld().getCRCRprtLstUdtCoAddrSt());
        rscWarnGnlHistDataRs.setCRCRprtHistRsdcAddrSt(rs.getQryGnlHistDataOld().getCRCRprtHistRsdcAddrSt());
        rscWarnGnlHistDataRs.setCRCRprtHistCoAddrSt(rs.getQryGnlHistDataOld().getCRCRprtHistCoAddrSt());
        rscWarnGnlHistDataRs.setRctly1MnthQryTmsOvr10(rs.getQryGnlHistDataOld().getRctly1MnthQryTmsOvr10());
        rscWarnGnlHistDataRs.setCRCRprtDgtlExplnGrdOvr700(rs.getQryGnlHistDataOld().getCRCRprtDgtlExplnGrdOvr700());
        rscWarnGnlHistDataRs.setQryGnlDataTm(rs.getQryGnlHistDataOld().getQryGnlDataTm());
        rscWarnGnlHistDataRs.setIdCardHitCrtLoseCrdtList_1(rs.getQryGnlHistDataNew().getIdCardHitCrtLoseCrdtList_1());
        rscWarnGnlHistDataRs.setIdCardHitCrmList_1(rs.getQryGnlHistDataNew().getIdCardHitCrmList_1());
        rscWarnGnlHistDataRs.setIdCardHitCrtExecList_1(rs.getQryGnlHistDataNew().getIdCardHitCrtExecList_1());
        rscWarnGnlHistDataRs.setMblNoHitFalNoLib_1(rs.getQryGnlHistDataNew().getMblNoHitFalNoLib_1());
        rscWarnGnlHistDataRs.setMblNoHitHghRskAttnList_1(rs.getQryGnlHistDataNew().getMblNoHitHghRskAttnList_1());
        rscWarnGnlHistDataRs.setMblNoHitArsCoLglRprsList_1(rs.getQryGnlHistDataNew().getMblNoHitArsCoLglRprsList_1());
        rscWarnGnlHistDataRs.setThreeMosIdCardLnkMultFlg_1(rs.getQryGnlHistDataNew().getThreeMosIdCardLnkMultFlg_1());
        rscWarnGnlHistDataRs.setSevenDayAppAplcMulBrrwFlg_1(rs.getQryGnlHistDataNew().getSevenDayAppAplcMulBrrwFlg_1());
        rscWarnGnlHistDataRs.setOneMosAplcMultPlatAppBrrwFlg_1(rs.getQryGnlHistDataNew().getOneMosAplcMultPlatAppBrrwFlg_1());
        rscWarnGnlHistDataRs.setThreeMosAplcMultPlatAppBrrwFlg_1(rs.getQryGnlHistDataNew().getThreeMosAplcMultPlatAppBrrwFlg_1());
        rscWarnGnlHistDataRs.setHitCardCntrPBCPshBlklist_1(rs.getQryGnlHistDataNew().getHitCardCntrPBCPshBlklist_1());
        rscWarnGnlHistDataRs.setHitCardCntrHPCExecPrsnList_1(rs.getQryGnlHistDataNew().getHitCardCntrHPCExecPrsnList_1());
        rscWarnGnlHistDataRs.setHitCardCntrHPCLoseCrdtPrsnList_1(rs.getQryGnlHistDataNew().getHitCardCntrHPCLoseCrdtPrsnList_1());
        rscWarnGnlHistDataRs.setHitCardBnkFrdBlklist_1(rs.getQryGnlHistDataNew().getHitCardBnkFrdBlklist_1());
        rscWarnGnlHistDataRs.setHitCardBnkSzLmtRskFlgBlklist_1(rs.getQryGnlHistDataNew().getHitCardBnkSzLmtRskFlgBlklist_1());
        rscWarnGnlHistDataRs.setHitCardCntrP2PLoseCrdtList_1(rs.getQryGnlHistDataNew().getHitCardCntrP2PLoseCrdtList_1());
        rscWarnGnlHistDataRs.setHitCardCntrBnkRskCardBlklist_1(rs.getQryGnlHistDataNew().getHitCardCntrBnkRskCardBlklist_1());
        rscWarnGnlHistDataRs.setHitCardCntrAbnTxnBlklist_1(rs.getQryGnlHistDataNew().getHitCardCntrAbnTxnBlklist_1());
        rscWarnGnlHistDataRs.setHitCardCntrLoanRskMrkBlklist_1(rs.getQryGnlHistDataNew().getHitCardCntrLoanRskMrkBlklist_1());
        rscWarnGnlHistDataRs.setHitCardCntrHistQryIndvBlklist_1(rs.getQryGnlHistDataNew().getHitCardCntrHistQryIndvBlklist_1());
        rscWarnGnlHistDataRs.setHitCardCntrHistQrySmShldGrylst_1(rs.getQryGnlHistDataNew().getHitCardCntrHistQrySmShldGrylst_1());
        rscWarnGnlHistDataRs.setHitCardCntrPBCPublcInfBadList_1(rs.getQryGnlHistDataNew().getHitCardCntrPBCPublcInfBadList_1());
        rscWarnGnlHistDataRs.setHitCardCntrFinBsnM4CustList_1(rs.getQryGnlHistDataNew().getHitCardCntrFinBsnM4CustList_1());
        rscWarnGnlHistDataRs.setHitHeadOfcBlklist_1(rs.getQryGnlHistDataNew().getHitHeadOfcBlklist_1());
        rscWarnGnlHistDataRs.setHitHeadOfcGrylst_1(rs.getQryGnlHistDataNew().getHitHeadOfcGrylst_1());
        rscWarnGnlHistDataRs.setHitAntiMnyLndrngMontrngList_1(rs.getQryGnlHistDataNew().getHitAntiMnyLndrngMontrngList_1());
        rscWarnGnlHistDataRs.setHitIDEAHistRfsList_1(rs.getQryGnlHistDataNew().getHitIDEAHistRfsList_1());
        rscWarnGnlHistDataRs.setOwnBnkLoanCrnOdueRcrd_1(rs.getQryGnlHistDataNew().getOwnBnkLoanCrnOdueRcrd_1());
        rscWarnGnlHistDataRs.setOwnBnkLoantHghstOdueOvr60Day_1(rs.getQryGnlHistDataNew().getOwnBnkLoantHghstOdueOvr60Day_1());
        rscWarnGnlHistDataRs.setLoanCrnStOdueFlg_1(rs.getQryGnlHistDataNew().getLoanCrnStOdueFlg_1());
        rscWarnGnlHistDataRs.setCrCardDiscvrFrzSpclFlg_1(rs.getQryGnlHistDataNew().getCrCardDiscvrFrzSpclFlg_1());
        rscWarnGnlHistDataRs.setCrCardDiscvrStopPymtSpclFlg_1(rs.getQryGnlHistDataNew().getCrCardDiscvrStopPymtSpclFlg_1());
        rscWarnGnlHistDataRs.setQscrdtDiscvrFrzSpclFlg_1(rs.getQryGnlHistDataNew().getQscrdtDiscvrFrzSpclFlg_1());
        rscWarnGnlHistDataRs.setQscrdtDiscvrStopPymtSpclFlg_1(rs.getQryGnlHistDataNew().getQscrdtDiscvrStopPymtSpclFlg_1());
        rscWarnGnlHistDataRs.setCrCard12MosOvr6TmsOdueFlg_1(rs.getQryGnlHistDataNew().getCrCard12MosOvr6TmsOdueFlg_1());
        rscWarnGnlHistDataRs.setQscrdt12MosOvr6TmsOdueFlg_1(rs.getQryGnlHistDataNew().getQscrdt12MosOvr6TmsOdueFlg_1());
        rscWarnGnlHistDataRs.setCrCard12MosOvr6TmsOdueRcrdFlg_1(rs.getQryGnlHistDataNew().getCrCard12MosOvr6TmsOdueRcrdFlg_1());
        rscWarnGnlHistDataRs.setCrCardRctly12MnthCtns3TmsOvOdue_1(rs.getQryGnlHistDataNew().getCrCardRctly12MnthCtns3TmsOvOdue_1());
        rscWarnGnlHistDataRs.setQscrdtRctly12MnthCtns3TmsOvOdue_1(rs.getQryGnlHistDataNew().getQscrdtRctly12MnthCtns3TmsOvOdue_1());
        rscWarnGnlHistDataRs.setLoan12MosOvr3TmsOdueRcrdFlg_1(rs.getQryGnlHistDataNew().getLoan12MosOvr3TmsOdueRcrdFlg_1());
        rscWarnGnlHistDataRs.setLoan12MnthHghstOdueTrmOvr2Tms_1(rs.getQryGnlHistDataNew().getLoan12MnthHghstOdueTrmOvr2Tms_1());
        rscWarnGnlHistDataRs.setLoan24MnthHghstOdueTrmOvr3Tms_1(rs.getQryGnlHistDataNew().getLoan24MnthHghstOdueTrmOvr3Tms_1());
        rscWarnGnlHistDataRs.setCrCard12MnthHghstOdueTrmOvr2Tms_1(rs.getQryGnlHistDataNew().getCrCard12MnthHghstOdueTrmOvr2Tms_1());
        rscWarnGnlHistDataRs.setQscrdt12MnthHghstOdueTrmOvr2Tms_1(rs.getQryGnlHistDataNew().getQscrdt12MnthHghstOdueTrmOvr2Tms_1());
        rscWarnGnlHistDataRs.setCrCard24MnthHghstOdueTrmOvr3Tms_1(rs.getQryGnlHistDataNew().getCrCard24MnthHghstOdueTrmOvr3Tms_1());
        rscWarnGnlHistDataRs.setQscrdt24MnthHghstOdueTrmOvr3Tms_1(rs.getQryGnlHistDataNew().getQscrdt24MnthHghstOdueTrmOvr3Tms_1());
        rscWarnGnlHistDataRs.setCRCRprtOwndFlg_1(rs.getQryGnlHistDataNew().getCRCRprtOwndFlg_1());
        rscWarnGnlHistDataRs.setCrRfrncRprtOwndHistTxnDataFlg_1(rs.getQryGnlHistDataNew().getCrRfrncRprtOwndHistTxnDataFlg_1());
        rscWarnGnlHistDataRs.setAplcEtrNatnlCrtLoseCrdtBeExecPrsnList_1(rs.getQryGnlHistDataNew().getAplcEtrNatnlCrtLoseCrdtBeExecPrsnList_1());
        rscWarnGnlHistDataRs.setPBCRprtInclAdmntvPnyRcrd_1(rs.getQryGnlHistDataNew().getPBCRprtInclAdmntvPnyRcrd_1());
        rscWarnGnlHistDataRs.setLoanSpclTxnTpInclDbtFlg_1(rs.getQryGnlHistDataNew().getLoanSpclTxnTpInclDbtFlg_1());
        rscWarnGnlHistDataRs.setRlvnRpymtRspblLvl5ClAttnSecdySspcsLss_1(rs.getQryGnlHistDataNew().getRlvnRpymtRspblLvl5ClAttnSecdySspcsLss_1());
        rscWarnGnlHistDataRs.setNotEndCvlExecRcrdFlg_1(rs.getQryGnlHistDataNew().getNotEndCvlExecRcrdFlg_1());
        rscWarnGnlHistDataRs.setRctlyExecRcrdFlngTmNotOvr180Day_1(rs.getQryGnlHistDataNew().getRctlyExecRcrdFlngTmNotOvr180Day_1());
        rscWarnGnlHistDataRs.setNonCnslAcctCrCardRctly6MnthAvUseLmt_1(rs.getQryGnlHistDataNew().getNonCnslAcctCrCardRctly6MnthAvUseLmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getNonCnslAcctCrCardRctly6MnthAvUseLmt_1()));
        rscWarnGnlHistDataRs.setNonCnslAcctQscrdt6MnthAvUseLmt_1(rs.getQryGnlHistDataNew().getNonCnslAcctQscrdt6MnthAvUseLmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getNonCnslAcctQscrdt6MnthAvUseLmt_1()));
        rscWarnGnlHistDataRs.setNonCnslAcctCrCardCrTotAmt_1(rs.getQryGnlHistDataNew().getNonCnslAcctCrCardCrTotAmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getNonCnslAcctCrCardCrTotAmt_1()));
        rscWarnGnlHistDataRs.setNonCnslAcctQscrdtCrTotAmt_1(rs.getQryGnlHistDataNew().getNonCnslAcctQscrdtCrTotAmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getNonCnslAcctQscrdtCrTotAmt_1()));
        rscWarnGnlHistDataRs.setAllCrCardCrTotAmt_1(rs.getQryGnlHistDataNew().getAllCrCardCrTotAmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getAllCrCardCrTotAmt_1()));
        rscWarnGnlHistDataRs.setAllQscrdtCrTotAmt_1(rs.getQryGnlHistDataNew().getAllQscrdtCrTotAmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getAllQscrdtCrTotAmt_1()));
        rscWarnGnlHistDataRs.setUtlzRt_1(rs.getQryGnlHistDataNew().getUtlzRt_1());
        rscWarnGnlHistDataRs.setCrTotAmt_1(rs.getQryGnlHistDataNew().getCrTotAmt_1() == null ? BigDecimal.ZERO : BigDecimal.valueOf(rs.getQryGnlHistDataNew().getCrTotAmt_1()));
        rscWarnGnlHistDataRs.setCRCRprtLstUdtRsdcAddr_1(rs.getQryGnlHistDataNew().getCRCRprtLstUdtRsdcAddr_1());
        rscWarnGnlHistDataRs.setCRCRprtLstUdtCoAddrSt_1(rs.getQryGnlHistDataNew().getCRCRprtLstUdtCoAddrSt_1());
        rscWarnGnlHistDataRs.setCRCRprtHistRsdcAddrSt_1(rs.getQryGnlHistDataNew().getCRCRprtHistRsdcAddrSt_1());
        rscWarnGnlHistDataRs.setCRCRprtHistCoAddrSt_1(rs.getQryGnlHistDataNew().getCRCRprtHistCoAddrSt_1());
        rscWarnGnlHistDataRs.setRctly1MnthQryTmsOvr10_1(rs.getQryGnlHistDataNew().getRctly1MnthQryTmsOvr10_1());
        rscWarnGnlHistDataRs.setCRCRprtDgtlExplnGrdOvr700_1(rs.getQryGnlHistDataNew().getCRCRprtDgtlExplnGrdOvr700_1());
        rscWarnGnlHistDataRs.setQryGnlDataTm_1(rs.getQryGnlHistDataNew().getQryGnlDataTm_1());
        logger.info("将接口返回的数据组装成相应的对象结束！组装后信息为：" + JSONObject.toJSONString(rscWarnGnlHistDataRs));
        return rscWarnGnlHistDataRs;
    }

    public boolean getCurrRscWarnLoanDatas(RscWarnLoanVO inRscWarnLoanVo) throws Exception {
        boolean flag = true;
        QueryRscWarnLoanReqBean queryRscWarnLoanReqBean = new QueryRscWarnLoanReqBean();
        //获取UUID作为流水号
        String reqId = UUID.randomUUID().toString().replace("-","");
        //通过借据号获取逾期时长/期数
        AccLoanVO DbllInfo = accLoanService.queryDbllInfo(inRscWarnLoanVo.getBillNo());
        if (Objects.nonNull(DbllInfo)) {
            if (DbllInfo.getOverdueDays() == null) throw new Exception("通过借据号(" + inRscWarnLoanVo.getBillNo() + ")未得到“逾期天数”！");
            if (DbllInfo.getMaxTimesTotal() == null) throw new Exception("通过借据号(" + inRscWarnLoanVo.getBillNo() + ")未得到“最高逾期期数！");
            queryRscWarnLoanReqBean.setDbllHghstOdueTrmCnt(DbllInfo.getMaxTimesTotal().toString());
            queryRscWarnLoanReqBean.setDbllCrnOdueDays(DbllInfo.getOverdueDays().toString());
        } else {
            throw new Exception("通过借据号(" + inRscWarnLoanVo.getBillNo() + ")未查询到借据信息！");
        }
        queryRscWarnLoanReqBean.setDbllNo(inRscWarnLoanVo.getBillNo());
        queryRscWarnLoanReqBean.setCustId(inRscWarnLoanVo.getCusId());
        queryRscWarnLoanReqBean.setCustNm(inRscWarnLoanVo.getCusName());
        queryRscWarnLoanReqBean.setIdentNo(inRscWarnLoanVo.getCertCode());
        queryRscWarnLoanReqBean.setIdentTp(inRscWarnLoanVo.getCertType());
        queryRscWarnLoanReqBean.setRqsIdmptntID(reqId);
        queryRscWarnLoanReqBean.setCardNo("");
        logger.info("查询条件为：" +  JSONObject.toJSONString(queryRscWarnLoanReqBean));
        QueryRscWarnLoanRespBean rs = queryRscWarnLoanService.applyQueryRscWarnLoan(queryRscWarnLoanReqBean);
        logger.info("查询结果为：" + JSONObject.toJSONString(rs));
        if (!reqId.equals(rs.getRqsIdmptntID())) flag = false;
        return flag;
    }

    public List<RscWarnLoanVO> queryRecRscWarnLoan(RscWarnLoanVO inRscWarnLoanVo, RscWarnLoanVO queryOldRs) throws Exception {
        logger.info("开始获取更新后的客户的风险信息！");
        logger.info("待修改的历史数据为：" + JSONObject.toJSONString(queryOldRs));
        logger.info("获取到的数据查询条件为：" + JSONObject.toJSONString(inRscWarnLoanVo));
        List<RscWarnLoanVO> queryRs = new ArrayList<>();
        long t1 = System.currentTimeMillis();
        int i = 1;
        while (true) {
            Thread.sleep(1000);
            long t2 = System.currentTimeMillis();
            if (t2 - t1 > 8000) {
                break;
            } else {
                queryRs = rscWarnLoanService.queryByCondByPage(inRscWarnLoanVo);
                logger.info("第" + i+ "次查询，结果为修改前的数据：" + JSONObject.toJSONString(queryRs));
                if (queryRs.size() > 0) {
                    if (!StringUtil.isNullorBank(queryRs.get(0).getTaskNo())) {
                        if (!queryRs.get(0).getTaskNo().equals(queryOldRs.getTaskNo())) {
                            logger.info("第" + i + "次查询，查询到修改后的结果！结果为：" + JSONObject.toJSONString(queryRs));
                            return queryRs;
                        }
                    }
                }
                i += 1;
            }
        }
        return null;
    }

    private List<CreditQueryDetailVO> getReportIdRs(RscWarnLoanVO inRscWarnLoanVo) throws Exception {
        CreditQueryDetailVO queryCon = new CreditQueryDetailVO();
        queryCon.setCusId(inRscWarnLoanVo.getCusId());
        queryCon.setCertCode(inRscWarnLoanVo.getCertCode());
        List<CreditQueryDetailVO> queryReportIdRsList = Optional.ofNullable(creditQueryDetailService.queryReportId(queryCon)).orElseThrow(() -> new Exception("没有查询到征信报告号！"));
        return queryReportIdRsList;
    }

    public ZXHtmlInfoVO getZXHtmlReport(RscWarnLoanVO inRscWarnLoanVo) throws Exception {
        //根据客户信息得到征信报告号
        List<CreditQueryDetailVO> queryReportIdRsList = getReportIdRs(inRscWarnLoanVo);
        //查询征信报告
        if (queryReportIdRsList.size() < 1) throw new Exception("没有征信报告！");
        ZXHtmlInfoVO queryoutputVo = new ZXHtmlInfoVO();
        queryoutputVo.setReportId(queryReportIdRsList.get(0).getReportId());
        ZXHtmlInfoVO outputVo = zxHtmlInfoService.queryByPk(queryoutputVo);
        return outputVo;
    }

    public ZXHtmlInfoVO getHistZXHtmlReport(RscWarnLoanVO inRscWarnLoanVo) throws Exception {
        //根据客户信息得到征信报告号
        List<CreditQueryDetailVO> queryReportIdRsList = getReportIdRs(inRscWarnLoanVo);
        //查询征信报告
        if (queryReportIdRsList.size() <= 1) throw new Exception("没有历史征信报告！");
        ZXHtmlInfoVO queryoutputVo = new ZXHtmlInfoVO();
        queryoutputVo.setReportId(queryReportIdRsList.get(1).getReportId());
        ZXHtmlInfoVO outputVo = zxHtmlInfoService.queryByPk(queryoutputVo);
        return outputVo;
    }

    public List<ZXHtmlInfoVO> getZXHtmlReports(RscWarnLoanVO inRscWarnLoanVo) throws Exception {
        //根据客户信息得到征信报告号
        List<CreditQueryDetailVO> queryReportIdRsList = getReportIdRs(inRscWarnLoanVo);
        List<String> reportIds = null;
        if (Objects.nonNull(queryReportIdRsList) && !queryReportIdRsList.isEmpty())
            reportIds = queryReportIdRsList.stream().map(a->{return a.getReportId();}).collect(Collectors.toList());
        if (Objects.isNull(reportIds) || reportIds.isEmpty()) throw new Exception("没有查询征信的记录！");
        List<ZXHtmlInfoVO> zXHtmlInfos = zxHtmlInfoService.queryAllZXHtmlReports(reportIds);
        if (Objects.isNull(zXHtmlInfos)) throw new Exception("没有征信报告！");
        return zXHtmlInfos;
    }

    public List<ZXHtmlInfoVO> getZXHtmlReportsByCus(RscWarnLoanVO inRscWarnLoanVo) throws Exception{
        ZXHtmlInfoVO param =new ZXHtmlInfoVO();
        param.setCertCode(inRscWarnLoanVo.getCertCode());
        List<ZXHtmlInfoVO> zXHtmlInfos = zxHtmlInfoService.queryZXHtmlReportsByCus(param);
        if (Objects.isNull(zXHtmlInfos)) throw new Exception("没有征信报告！");
        return zXHtmlInfos;
    }
}
